#coding=utf-8

import sys, getopt

from Checker.MZCCFileChecker import mzccFileCheck
from Checker.MZCCDirChecker import *
from Misc.MZCCMisc import *
from Analysis.MZCCAnalysis import *
from Config.MZCCRuleParser import *

#检查主体(Target)：三者选其一
optT_filePath = "";         # 待查文件目录（单文件），默认空
#optT_filePath = "test.txt";
optT_fileListFile = "";     # 待查列表文件，默认空
#optT_fileListFile = "testWhiteList.txt";
optT_dirPath = "";          # 待查目录（单目录），默认空
#optT_dirPath = "/Users/chris/Documents/gitWorkingCopys/MZAppLog/MZLog/Classes";
#optT_dirPath = "/Users/chris/Documents/gitWorkingCopys/MaiziLoan/MaiZiFinance";
#optT_dirPath = "/Users/chris/Documents/gitWorkingCopys/nonobank/NuoNuoBank_App_git/YHB_Prj/NNProjectModule/ProductModule"
#optT_dirPath = "/Users/chris/Documents/gitWorkingCopys/nonobank/NuoNuoBank_App_git/YHB_Prj/NNProjectModule"

#输入配置(Input Config)
optIC_problemNumThre = 1000;            # 问题个数阈值，默认：1000
optIC_blackListFile = "";               # 黑名单文件路径（仅对目录检查生效），默认空
#optIC_blackListFile = "testBlackList.txt";
optIC_whiteListFile = "";               # 白名单文件路径（仅对目录检查生效），默认空
#optIC_whiteListFile = "testWhiteList.txt";
optIC_enabledSuffix = ".h,.m";          # 有效后缀（仅对目录检查生效），默认：.h,.m
optIC_rulePackagePath = "./Config";     # 规则路径，默认：Config

#输出配置(Output Config)
# 默认输出到当前目录下的Results文件夹，不可配置

(opts, args) = getopt.getopt(sys.argv[1:], "f:l:d:t:b:w:s:")

for tmpOp, tmpVal in opts:

    if "-f" == tmpOp:
        optT_filePath = tmpVal;
    if "-l" == tmpOp:
        optT_fileListFile = tmpVal;
    if "-d" == tmpOp:
        optT_dirPath = tmpVal;

    if "-t" == tmpOp:
        optIC_problemNumThre = int(tmpVal);
    if "-b" == tmpOp:
        optIC_blackListFile = tmpVal;
    if "-w" == tmpOp:
        optIC_whiteListFile = tmpVal;
    if "-s" == tmpOp:
        optIC_enabledSuffix = tmpVal;

    # 对自定义规则函数的引入方案暂未想出
    # if "-r" == tmpOp:
    #     optIC_rulePackagePath = tmpVal;

tmpIC_ruleJsonPath = optIC_rulePackagePath + "/mzcc_rules.json";
tmpIC_sinLineRules = mzccSingleLineRuleList(tmpIC_ruleJsonPath);
tmpIC_mulLineRules = mzccMulLineRuleList(tmpIC_ruleJsonPath);

out_problemDic = {};
out_checkList = [];
out_whiteList = [];

tmpCheckType = "9999";  #检查类型
tmpCheckPath = "";      #检查目录

if optT_filePath != "":
    # 单文件检查
    tmpCheckType = "0";
    tmpCheckPath = optT_filePath;
    tmpProblemList = mzccFileCheck(optT_filePath,
                                   tmpIC_sinLineRules,
                                   tmpIC_mulLineRules);
    if len(tmpProblemList) != 0:
        out_problemDic[optT_filePath] = tmpProblemList;

elif optT_fileListFile != "":
    # 列表文件检查
    tmpCheckType = "1";
    tmpCheckPath = "[目标为特定文件列表]";
    out_checkList = mzccFileToList(optT_fileListFile);
    out_problemDic = mzccListCheck(out_checkList,
                                   optIC_problemNumThre,
                                   tmpIC_sinLineRules,
                                   tmpIC_mulLineRules);

elif optT_dirPath != "":
    # 目录文件检查
    tmpCheckType = "2";
    tmpCheckPath = optT_dirPath;
    tmpBlackList = mzccFileToList(optIC_blackListFile);
    tmpWhiteList = mzccFileToList(optIC_whiteListFile);
    suffixList = mzccSuffixListFromStr(optIC_enabledSuffix);
    out_problemDic = mzccDirCheck(optT_dirPath,
                                  tmpBlackList,
                                  tmpWhiteList,
                                  suffixList,
                                  optIC_problemNumThre,
                                  tmpIC_sinLineRules,
                                  tmpIC_mulLineRules);

#问题输出
mzccDoAnalysis(out_problemDic);
mzccWriteResult(tmpCheckType, tmpCheckPath, out_problemDic);
